package com.mnb.base.controller;

import com.mnb.base.entity.SysRole;
import com.mnb.base.entity.SysUser;
import com.mnb.base.service.SysRoleService;
import com.mnb.common.utils.R;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;


/**
 * (sys_role)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController {

    @Autowired
    private HttpSession session;

    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/pageList")
    public R pageList(SysRole sysRole) {
        return sysRoleService.getPageList(sysRole);
    }

    @RequestMapping("/list")
    public R list(SysRole sysRole) {
        return sysRoleService.list(sysRole);
    }

    @RequestMapping("/insert")
    public R insert(@RequestBody SysRole sysRole) {
        sysRole.setCreateBy(getUserName());
        sysRole.setCreateTime(new Date());
        return sysRoleService.insert(sysRole);
    }

    @RequestMapping("/udpate")
    public R udpate(@RequestBody SysRole sysRole) {
        return sysRoleService.udpate(sysRole);
    }

    @RequestMapping("/delete")
    public R delete(Long[] roleIds) {
        return sysRoleService.deleteRoleByIds(roleIds);
    }

    public String getUserName()
    {
        SysUser user = (SysUser) session.getAttribute("user");
        if(user == null){
            throw new RuntimeException("用户未登录");
        }
        return user.getUserName();
    }
}
